import { ref, computed } from 'vue'
var EllipsisID = ref("EllipsisID");
var isRemoveElement = ref(true);
var overTime = ref(0);
var outTime = ref(0);
var overTimer = ref(void 0);
var outTimer = ref(void 0);
var originParent = ref();
var zIndex = 0;
function useGlobalConfig(key2, defaultValue = void 0) {
	const config = ref(defaultValue);
	if (key2) {
		return computed(() => {
			var _a2, _b;
			return (_b = (_a2 = config.value) == null ? void 0 : _a2[key2]) != null ? _b : defaultValue;
		});
	} else {
		return config;
	}
}
var useZIndex = () => {
	const initialZIndex = useGlobalConfig("zIndex", 4e3);
	const currentZIndex = computed(() => initialZIndex.value + zIndex);
	const nextZIndex = () => {
		zIndex++;
		return currentZIndex.value;
	};
	return {
		initialZIndex,
		currentZIndex,
		nextZIndex
	};
};
function clearTime() {
	outTime.value = 0;
	overTime.value = 0;
	clearTimeout(overTimer.value);
	clearTimeout(outTimer.value);
}
function clearEllipsis() {
	let box = document.querySelector("#" + EllipsisID.value);
	box == null ? void 0 : box.remove();
}
function getScrollParent(element) {
	const overflowRegex = /(auto|scroll|hidden)/;
	// eslint-disable-next-line no-cond-assign
	for (let parent2 = element; parent2 = parent2.parentElement; parent2.parentElement !== document.body) {
		const style2 = window.getComputedStyle(parent2);
		if (overflowRegex.test(style2.overflow + style2.overflowX + style2.overflowY)) {
			return parent2;
		}
	}
	return window;
}
function onScrollEvent() {
	clearEllipsis();
}
function bindScrollEvent(e) {
	originParent.value = getScrollParent(e);
	originParent.value.addEventListener("scroll", onScrollEvent);
}
var EllipsisOverFun = (e, el, binding, disabled) => {
	var _a2;
	clearEllipsis();
	clearTime();
	if (disabled)
		return;
	isRemoveElement.value = true;
	overTime.value = (/* @__PURE__ */ new Date()).getTime();
	overTimer.value = setTimeout(() => {
		var _a22, _b, _c, _d, _e, _f, _g;
		if (document.querySelector("#" + EllipsisID.value)) {
			(_a22 = document.querySelector("#" + EllipsisID.value)) == null ? void 0 : _a22.remove();
		}
		const Ellipsis = document.createElement("div");
		Ellipsis.classList.add("ellipsis-title");
		if ((_b = binding.value) == null ? void 0 : _b.theme) {
			Ellipsis.classList.add((_c = binding.value) == null ? void 0 : _c.theme);
		}
		Ellipsis.id = EllipsisID.value;
		const maxWidth = 400;
		const EllipsisWidth = maxWidth;
		if (el.scrollWidth === 0) {
			return;
		}
		Ellipsis.style["max-width"] = `${EllipsisWidth}px`;
		if (window.innerWidth - e.clientX < maxWidth) {
			Ellipsis.style.right = `${window.innerWidth - e.clientX}px`;
		} else {
			Ellipsis.style.left = `${e.clientX + 20}px`;
		}
		if (window.innerHeight - e.clientY < 600) {
			Ellipsis.style.bottom = `${window.innerHeight - e.clientY}px`;
		} else {
			Ellipsis.style.top = `${e.clientY}px`;
		}
		Ellipsis.style["z-index"] = useZIndex().nextZIndex();
		Ellipsis.innerHTML = ((_d = binding.value) == null ? void 0 : _d.title) || el.innerHTML;
		if (((_e = binding.value) == null ? void 0 : _e.enterable) == void 0 || ((_f = binding.value) == null ? void 0 : _f.enterable)) {
			Ellipsis.addEventListener("mouseover", () => {
				isRemoveElement.value = false;
			});
			Ellipsis.addEventListener("mouseout", () => {
				Ellipsis.remove();
			});
		}
		bindScrollEvent(el);
		(_g = document.querySelector("body")) == null ? void 0 : _g.append(Ellipsis);
		clearTime();
	}, ((_a2 = binding.value) == null ? void 0 : _a2.time) || 300);
};
var EllipsisOutFun = (e, binding) => {
	var _a2;
	outTime.value = (/* @__PURE__ */ new Date()).getTime();
	if (outTime.value - overTime.value < 300) {
		clearTime();
	} else {
		outTimer.value = setTimeout(() => {
			if (isRemoveElement.value) {
				clearEllipsis();
				clearTime();
			}
		}, ((_a2 = binding.value) == null ? void 0 : _a2.time) || 300);
	}
};
export const vEllipsis = (app) => {
	app.directive('ellipsis', {
		mounted(el, binding) {
			var _a2;
			if ((_a2 = binding.value) == null ? void 0 : _a2.disabled)
				return;
			el.addEventListener("mouseover", (e) => {
				if (el.scrollWidth > el.offsetWidth || el.scrollHeight > el.offsetHeight) {
					EllipsisOverFun(e, el, binding);
				}
			});
			el.addEventListener("mouseout", (e) => {
				if (el.scrollWidth > el.offsetWidth || el.scrollHeight > el.offsetHeight) {
					EllipsisOutFun(e, binding);
				}
			});
		},
		updated(el, binding) {
			clearEllipsis();
			el.addEventListener("mouseover", (e) => {
				var _a2;
				if (el.scrollWidth > el.offsetWidth || el.scrollHeight > el.offsetHeight) {
					EllipsisOverFun(e, el, binding, (_a2 = binding.value) == null ? void 0 : _a2.disabled);
				}
			});
		}
	})

};